package com.velvetantler.weather.view.detail

import android.os.Bundle
import android.view.LayoutInflater
import androidx.core.net.toUri
import androidx.media3.common.MediaItem
import androidx.media3.exoplayer.ExoPlayer
import com.velvetantler.weather.base.BaseBindingActivity
import com.velvetantler.weather.databinding.AcMedia3PlayerBinding

class Media3PlayerActivity : BaseBindingActivity<AcMedia3PlayerBinding>() {

    private var exoPlayer: ExoPlayer? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun initBinding(layoutInflater: LayoutInflater): AcMedia3PlayerBinding {
        return AcMedia3PlayerBinding.inflate(layoutInflater)
    }

    override fun onStart() {
        super.onStart()

        initExoPlayer()

    }

    override fun onStop() {
        super.onStop()

        releaseExoPlayer()
    }

    fun initExoPlayer() {
        // 初始化player
        exoPlayer = ExoPlayer.Builder(this).build()
        // 先绑定播放器
        binding.playerView.player = exoPlayer
        // 设置播放地址 raw文件夹内的视频
        val videoUrl =
            "android.resource://${this.packageName}/raw/weather_demo".toUri()
        exoPlayer?.setMediaItem(MediaItem.fromUri(videoUrl))
        // 准备播放
        exoPlayer?.prepare()
    }

    fun releaseExoPlayer() {
        exoPlayer?.release()
    }
}